package com.yuexun.beilunpatient.ui.consult.ui;

import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseKJActivity;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.bean.BaseListEntity;
import com.yuexun.beilunpatient.ui.consult.adapter.MailFileAdapter;
import com.yuexun.beilunpatient.ui.consult.bean.MailFileBean;
import com.yuexun.beilunpatient.ui.consult.bean.MailFileDataBean;
import com.yuexun.beilunpatient.ui.consult.bean.NewsDetailBean;
import com.yuexun.beilunpatient.ui.consult.model.impl.ArticleModel;
import com.yuexun.beilunpatient.ui.consult.model.impl.HelpModel;
import com.yuexun.beilunpatient.ui.consult.presenter.impl.ArticleDetailPresenter;
import com.yuexun.beilunpatient.ui.consult.presenter.impl.HelpPresent;
import com.yuexun.beilunpatient.ui.consult.ui.view.IArticleDetailView;
import com.yuexun.beilunpatient.ui.consult.ui.view.IHelpView;
import com.yuexun.beilunpatient.utils.URLImageGetter;
import com.yuexun.beilunpatient.widget.FixedSwipeListView;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2018/10/29.
 */
public class Act_Consult_Detail extends BaseKJActivity implements IArticleDetailView, IHelpView {

    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.datetime)
    TextView datetime;
    @Bind(R.id.content)
    TextView content;
    @Bind(R.id.flv_attachment)
    FixedSwipeListView flvAttachment;

    ArticleDetailPresenter presenter;
    HelpPresent helpPresent;
    MailFileAdapter mailFileAdapter;

    ArrayList<MailFileDataBean> mailFileDataBeans = new ArrayList<MailFileDataBean>();

    @Override
    public void setRootView() {
        setContentView(R.layout.act_consult_detail);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        presenter = new ArticleDetailPresenter(new ArticleModel(), this);
        helpPresent = new HelpPresent(new HelpModel(), this);
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("newsId", getIntent().getExtras().getString("newsId"));
        presenter.inquireNewsDetailForIndex(params);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.title_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
        }
    }

    @Override
    public void inquireNewsDetailForIndex(BaseListEntity<NewsDetailBean> entity) {
        if(entity.getStatus().equals("200")){
            title.setText(entity.getDatas().get(0).getTitle());
            datetime.setText("发布时间:" + entity.getDatas().get(0).getCreateDate()+"      阅读量:"+entity.getDatas().get(0).getReadCount());
            content.setText(Html.fromHtml(entity.getDatas().get(0).getContent().replace("＜", "<").replace("＞", ">")
                    .replace(AppConfig.TEXTVIEW_WEB, AppConfig.TEXTVIEW_WEB_All), new URLImageGetter(content, aty), null));

            if(entity.getDatas().get(0).getFileUrl().length()>0) {
                String[] fileUrls = entity.getDatas().get(0).getFileUrl().split(";");
                if (fileUrls.length > 0) {
                    mailFileDataBeans.clear();
                    for (int i = 0; i < fileUrls.length; i++) {
                        Map<String, String> params = new HashMap<>();
                        params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
                        params.put("fileUuid", fileUrls[i]);
                        helpPresent.getFileData(params);
                    }
                }
            }
        }
    }

    @Override
    public void getFileData(MailFileBean mailFileBean) {
        if (mailFileBean.getStatus().equals("200")) {
            mailFileDataBeans.add(mailFileBean.getDatas().get(0));
            mailFileAdapter = new MailFileAdapter(this, mailFileDataBeans, false);
            flvAttachment.setAdapter(mailFileAdapter);
        } else {
            ViewInject.toast("文件信息获取失败");
        }
    }
}
